<template>
  <div :class="className" :style="{ height: height, width: width }" />

</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import { getRandomColor } from '@/utils'
const animationDuration = 2000
export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    title: {
      type: String,
      default: ''
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    chartOption: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      sidebarElm: null
    }
  },
  watch: {
    chartData: {
      immediate: true,
      deep: true,
      handler(val) {
         this.$nextTick(()=>{
          this.setOptions(val)
         })
      }
    }
  },
  mounted() {
    this.initChart()
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }

    // 监听侧边栏的变化
    this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }

    this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)

    this.chart.dispose()
    this.chart = null
  },
  methods: {
    sidebarResizeHandler(e) {
      if (e.propertyName === 'width') {
        this.__resizeHandler()
      }
    },
    setOptions(obj) {

      if(!obj){
        return
      }
      const option = {
        // backgroundColor: '#323a5e',
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          left: '2%',
          right: '4%',
          bottom: '14%',
          top: '16%',
          containLabel: true
        },
        legend: {
          data:['非常满意', '满意', '一般', '不满意','非常不满意'],
          right: 10,
          top: 12,
          textStyle: {
            color: "#000"
          },
          itemWidth: 12,
          itemHeight: 10,
          // itemGap: 35
        },
        xAxis: {
          type: 'category',
          data: ['数据完整准确性', '问题精准性及剖析深度', '重点突出程度', '对后续工作的指导性'],
          axisLabel: {
            textStyle: {
                fontSize: 16  // 设置X轴标签的字体大小为12px
            }
        }

        },

        yAxis: {
          type: 'value',
          axisLine: {
               show: true // 显示刻度线
           },
           minInterval: 1,

        },

        series: [{
          name: '非常满意',
          type: 'bar',
          barWidth: '10%',
          barGap:"70%",

          label: {
            show: true,
            position: "top",  // 展示在柱子的上方
            formatter:"非常满意",
            color:"#2EC7C9"
          },
          itemStyle: {
            normal: {
              // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              //   offset: 0,
              //   color: '#fccb05'
              // }, {
              //   offset: 1,
              //   color: '#f5804d'
              // }]),
             // barBorderRadius: 12,
            },
          },
         data: obj.data0

        },
        {
          name: '满意',
          type: 'bar',
          barWidth: '10%',
          barGap:"70%",
          label: {
            show: true,
            position: "top",  // 展示在柱子的上方
            formatter:"满意",
            color:"#B6A2DE"
          },
          itemStyle: {
            normal: {
              // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              //   offset: 0,
              //   color: '#8bd46e'
              // }, {
              //   offset: 1,
              //   color: '#09bcb7'
              // }]),
             // barBorderRadius: 11,
            }

          },
          data: obj.data1
        },
        {
          name: '一般',
          type: 'bar',
          barWidth: '10%',
          barGap:"70%",
          label: {
            show: true,
            position: "top",  // 展示在柱子的上方
            formatter:"一般",
            color:"#5AB1EF"
          },
          itemStyle: {
            normal: {
              // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              //   offset: 0,
              //   color: '#248ff7'
              // }, {
              //   offset: 1,
              //   color: '#6851f1'
              // }]),
              //barBorderRadius: 11,
            }
          },
          data:obj.data2
        },
        {
          name: '不满意',
          type: 'bar',
          barWidth: '10%',
          barGap:"70%",
          label: {
            show: true,
            position: "top",  // 展示在柱子的上方
            formatter:"不满意",
            color:"#FFB980"
          },
          itemStyle: {
            normal: {
              // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              //   offset: 0,
              //   color: '#248ff7'
              // }, {
              //   offset: 1,
              //   color: '#6851f1'
              // }]),
             // barBorderRadius: 11,
            }
          },
          data:obj.data3
        },
        {
          name: '非常不满意',
          type: 'bar',
          barWidth: '10%',
          barGap:"70%",
          label: {
            show: true,
            position: "top",  // 展示在柱子的上方
            formatter:"非常不\n满意",
            color:"#D87A80"
          },
          itemStyle: {
            normal: {
              // color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              //   offset: 0,
              //   color: '#248ff7'
              // }, {
              //   offset: 1,
              //   color: '#6851f1'
              // }]),
              // barBorderRadius: 11,
            }
          },
          data: obj.data4
        }
      ]
      };
      // const option = Object.assign({}, {
      //   title: {
      //     text: this.title
      //   },
      //   xAxis: [{
      //     type: 'category',
      //     data: xAxis,
      //     axisTick: {
      //       alignWithLabel: true
      //     }
      //   }],
      //   grid: {
      //     left: '3%',
      //     right: '4%',
      //     bottom: '3%',
      //     containLabel: true
      //   },
      //   tooltip: {
      //     trigger: 'axis',
      //     axisPointer: {
      //       type: 'cross'
      //     },
      //     padding: [5, 10]
      //   },
      //   toolbox: {
      //     feature: {
      //       restore: {},
      //       dataView: {},
      //       saveAsImage: {}
      //     },
      //   },
      //   yAxis: [{
      //     type: 'value',
      //     axisTick: {
      //       show: false
      //     }
      //   }],
      //   series: yAxis
      // })
      // console.log(option)
      this.chart.setOption(option)
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    }
  }
}
</script>
